package com.leenmvc.core.web.controller;

import com.leenmvc.core.annotation.Note;
import com.leenmvc.core.base.BaseController;
import com.leenmvc.core.security.User;
import com.leenmvc.core.security.dao.core.SysLanguageDao;
import com.leenmvc.core.security.dao.core.SysRoleMenuDao;
import com.leenmvc.core.security.dao.core.SysUserDao;
import com.leenmvc.core.security.entity.core.*;
import com.leenmvc.core.security.service.core.SysMenuService;
import com.leenmvc.core.utils.collection.TreeUtils;
import com.leenmvc.core.utils.http.HttpContextUtils;
import com.leenmvc.core.base.result.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统菜单控制器
 */
@RestController
@RequestMapping("system/sysMenu")
public class SysMenuController extends BaseController<SysMenu> {

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    private SysLanguageDao sysLanguageDao;

    @GetMapping("nav")
//    @RequiresPermissions("system:menu:nav")
    @Note("获取用户的菜单列表用于显示菜单栏")
    public Result nav() {
        List<SysMenu> userMenu = sysUserDao.getUserMenu(User.getId());
        return new Result().ok(userMenu);
    }

    @GetMapping("permissions")
    @Note("获取用户对应的所有权限，供前台缓存使用")
    public Result permissions() {
        Set<String> userPermissions = sysUserDao.getUserPermissions(User.getId());
        return new Result().ok(userPermissions);
    }

    @GetMapping("list")
    @Note("获取所有菜单信息list")
    public Result list() {
        List<SysMenu> menulist = listBySqlKey("getMenuList", "language", HttpContextUtils.getLanguage());
        for (SysMenu menu : menulist) {
            Long topId = TreeUtils.findTopId(menu);
            menu.setTopId(topId);
        }
        List<SysMenu> build = TreeUtils.build(menulist);
        return new Result().ok(build);
    }




    @GetMapping("{id}")
    @RequiresPermissions("system:menu:get")
    @Note("根据ID获取本工程的一个菜单信息")
    public Result getById(@PathVariable("id") Long id) {
        SysMenu menu = sysMenuService.get(id);
        Long topId = TreeUtils.findTopId(menu);;
        menu.setTopId(topId);
        return new Result().ok(menu);
    }

    @Note("获取未绑定的角色菜单")
    @GetMapping("getMenu")
    public Result getUnboundMenu(String roleId){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("roleId",roleId);
        List<SysMenu> all = listBySqlKey("getList", map);
        List<Long> permList = sysRoleMenuDao.listBySqlKey("getMenuIds", map);
        List<SysMenu> sysMenus = all.stream().filter(SysMenu -> !permList.contains(SysMenu.getId())).collect(Collectors.toList());
        return new Result().ok(sysMenus);
    }

    @GetMapping("getMenuListForRole")
    @Note("通过角色获取菜单的树结构,传入角色Id")
    public Result getMenuListForRole(@RequestParam Long roleId) {
        List<SysMenu> menulist = sysMenuService.getMenuListForRole(roleId);
        for (SysMenu menu : menulist) {
            Long topId = TreeUtils.findTopId(menu);
            menu.setTopId(topId);
        }
        List<SysMenu> build = TreeUtils.build(menulist);
        return new Result().ok(build);
    }

}
